package com.mida.bms.report.service.impl;

import com.mida.bms.report.mapper.DocDefineSpaceConsumerMapper;
import com.mida.bms.report.service.DocDefineSpaceConsumerService;
import com.mida.bms.report.vo.DocDefineSpaceConsumerVo;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.mida.tool.common.result.BaseResult.buildListSuccess;
import static com.mida.tool.common.result.BaseResult.buildListWarn;

/**
 * @ClassName: DocDefineSpaceConsumerServiceImpl
 * @Description: TODO
 * @Author: wang xiao le
 * @Date: 2021/6/16 13:19
 **/
@Service
@Slf4j
@Transactional(value = "docTxManager", rollbackFor = Exception.class)
public class DocDefineSpaceConsumerServiceImpl implements DocDefineSpaceConsumerService {


    @Autowired
    private DocDefineSpaceConsumerMapper docDefineSpaceConsumerMapper;

    /**
     * 1、根据定义空间soid查询定义空间用户信息
     *
     * @param defineSpaceSoid 定义空间soid
     * @return 定义空间用户信息集合
     */
    @Override
    public ListResult<List<DocDefineSpaceConsumerVo>> selectConsumersByDefineSpaceSoid(long defineSpaceSoid) {
        log.info("开始根据定义空间soid查询定义空间用户信息,查询条件:" + defineSpaceSoid);
        ListResult listResult = new ListResult();
        StopWatch stopWatch = StopWatch.createStarted();
        List<DocDefineSpaceConsumerVo> docDefineSpaceConsumers = null;
        try {
            docDefineSpaceConsumers = docDefineSpaceConsumerMapper.selectConsumersByDefineSpaceSoid(defineSpaceSoid);
            if (docDefineSpaceConsumers == null) {
                docDefineSpaceConsumers = new ArrayList<DocDefineSpaceConsumerVo>(0);
            }
            buildListSuccess(listResult, docDefineSpaceConsumers);
        } catch (Exception e) {
            log.error("根据定义空间soid查询定义空间用户信息异常", e);
            buildListWarn(listResult, "根据定义空间soid查询定义空间用户信息失败");
        } finally {
            stopWatch.stop();
            log.info("本次查询耗时:" + stopWatch.getTime() + "ms");
        }
        return listResult;
    }
}
